package DAL;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class SQLConnection {
    	//Configuration de la connection
	private String databaseURL = "";
	private String user = "";
	private String password = "";
	private Connection connection;
	private Statement query;
        private ResultSet resultat;
        
        //Définition du driver
	static {
		try {
			Class.forName("");
		} catch (ClassNotFoundException e) {
			throw new RuntimeException(e);
		}
	}
	
	//Connecter
	public void connecter() {
		try {
			connection = DriverManager.getConnection(databaseURL, user, password);
			connection.setAutoCommit(false);
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}
	
	//Deconnecter
        public void deconnecter() {
		try {
			if (connection != null)
				connection.rollback();
			if (query != null)
				query.close();
			if (connection != null)
				connection.close();
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}
        
        //Executer une query de recherche
        public ResultSet executeSearch(String query){
                try {
                        this.query = connection.createStatement();
                        this.resultat = this.query.executeQuery(query);
                        return this.resultat;
                } catch (SQLException e) {
                        throw new RuntimeException(e);
                }
        }
        
        //Executer une query
        public void executeQuery(String query){
                try {
                        this.query = connection.createStatement();
                        this.resultat = this.query.executeQuery(query);
                        connection.commit();
                } catch (SQLException e) {
                        throw new RuntimeException(e);
                }
        }
        
        //Getter
        public Connection getConnection () {
                return connection;
        }
}